const express = require("express");
const router = express.Router();
const article_handler = require("../router_handler/article");
//导入解析formdata格式表单数据的包
const multer = require("multer");
//导入处理路径的核心模块
const path = require("path");
// 创建multer的实例对象,通过dest属性指定文件的存放路径
const upload = multer({ dest: path.join(__dirname, "../uploads") });
// 导入验证数据的中间件
const expressJoi = require("@escook/express-joi");
// 导入文章的验证模块
const schema = require("../sechma/article");
// 发布新文章
// upload.single() 是一个局部生效的中间件，用来解析 FormData 格式的表单数据
// 将文件类型的数据，解析并挂载到 req.file 属性中
// 将文本类型的数据，解析并挂载到 req.body 属性中
router.post(
  "/add",
  upload.single("cover_img"),
  expressJoi(schema.add_article_schema),
  article_handler.addArticle
);
// 获取文章的列表数据
router.get(
  "/list",
  expressJoi(schema.get_article_schema),
  article_handler.getArticleList
);
// 根据id删除文章数据
router.get(
  "/delete/:id",
  expressJoi(schema.del_article_schema),
  article_handler.delArticle
);
router.get(
  "/:id",
  expressJoi(schema.del_article_schema),
  article_handler.getArticle
);
router.post(
  "/edit",
  upload.single("cover_img"),
  expressJoi(schema.update_article_schema),
  article_handler.updateArticle
);
module.exports = router;
